package com.Sep_Eight;

public class Solution513 {

    int maxDepth=Integer.MIN_VALUE;
    int minVal=0;

    public int findBottomLeftValue(TreeNode root) {
        if (root==null) return 0;
        dfs(root,0);
        return minVal;
    }

    public void dfs(TreeNode root,int leftLen){
        if (root.left==null&&root.right==null)
        {
            if (leftLen>maxDepth)
            {
                maxDepth=leftLen;
                minVal=root.val;
            }
            return;
        }
        if (root.left!=null)
        {
            dfs(root.left,leftLen+1);
        }
        if (root.right!=null)
        {
            dfs(root.right,leftLen+1);
        }
    }

    private class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
